package com.example.accessibility;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.util.Log;




import android.view.accessibility.AccessibilityNodeInfo;


import com.taobao.weex.common.WXModule;

import org.json.JSONObject;

import java.util.List;

import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

public class UniAccessibility extends WXModule {
    public MyAccess myAccess=new MyAccess();


    @UniJSMethod(uiThread = false)
    public void JumpToAccess(){

        if (!MyAccess.isStart())
        {
            try {
                mUniSDKInstance.getContext().startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
            } catch (Exception e) {
                mUniSDKInstance.getContext().startActivity(new Intent(Settings.ACTION_SETTINGS));
                e.printStackTrace();
            }
            return;
        }

    }
    
    @UniJSMethod(uiThread = false)
    public void kuaishou(String text,int image_num1,UniJSCallback callback)
    {
        Intent intent = mUniSDKInstance.getContext().getPackageManager().getLaunchIntentForPackage("com.smile.gifmaker");
        mUniSDKInstance.getContext().startActivity(intent);

        myAccess.set_Click(new String[]{"com.smile.gifmaker:id/shoot_container"});
        myAccess.set_Click(new String[]{"相册"});
        myAccess.setClick_seriescallback(new MyAccess.click_seriescallback() {
            @Override
            public void onclickseriescall() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                myAccess.fine_view(new String[]{"com.smile.gifmaker:id/media_pick_num_area"}, new MyAccess.FindCallback() {
                    @Override
                    public void onfindcall(List<AccessibilityNodeInfo> list) {
                        int image_num=image_num1;
                        if (image_num>list.size())
                        {
                            image_num=list.size();
                        }
                        for (int i = 0; i < image_num; i++) {
                            DarknessMagic.AccessibilityServiceUtils.clickView(list.get(i));
                        }
                        myAccess.set_Click(new String[]{"下一步","com.smile.gifmaker:id/next_step"});
                        myAccess.set_Click(new String[]{"text: 下一步;"});
                        myAccess.setClick_seriescallback(new MyAccess.click_seriescallback() {
                            @Override
                            public void onclickseriescall() {
                                myAccess.fine_view(new String[]{"com.smile.gifmaker:id/editor","添加合适的话题和描述，作品能获得更多推荐"}, new MyAccess.FindCallback() {
                                    @Override
                                    public void onfindcall(List<AccessibilityNodeInfo> info) {
                                        if (info.size()!=0)
                                        {
                                            ClipboardManager clipboard = (ClipboardManager)mUniSDKInstance.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                                            ClipData clip = ClipData.newPlainText("text", text);
                                            clipboard.setPrimaryClip(clip);
                                            info.get(0).performAction(AccessibilityNodeInfo.ACTION_FOCUS);
                                            info.get(0).performAction(AccessibilityNodeInfo.ACTION_PASTE);
                                            callback.invoke(true);
                                            callback.invokeAndKeepAlive(true);
                                        }
                                    }
                                });
                            }
                        });
                    }
                });
            }
        });

    }
    @UniJSMethod(uiThread = false)
    public void toapp(String string,Context context)
    {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(string);
        context.startActivity(intent);

    }



}
